/**
 * 存储数据
 */
export const setItem = (key: string, value: string, type?: number) => {
  // 将数组、对象类型的数据转化为 JSON 字符串进行存储
  if (typeof value === 'object') {
    value = JSON.stringify(value);
  }
  if (type) {
    localStorage.setItem(key, value);
  } else {
    sessionStorage.setItem(key, value);
  }
};
/**
 * 获取数据
 */
export const getItem = (key: string) => {
  let data: string = window.sessionStorage.getItem(key) as string;
  if (data === '' || data === null) {
    data = window.localStorage.getItem(key) as string;
  }
  try {
    return JSON.parse(data);
  } catch (err) {
    return data;
  }
};

/**
 * 删除数据
 */
export const removeItem = (key: string) => {
  window.localStorage.removeItem(key);
  window.sessionStorage.removeItem(key);
};

/**
 * 删除所有数据
 */
export const removeAllItem = () => {
  window.localStorage.clear();
  window.sessionStorage.clear();
};
